var userName;
var loginStatus;
var wordId, word, mean, sent;
var lastclicked;
var meanShown = 0;

function fetchWord(){
	$.post('scripts/fetchword.php', function(data){
		var temp = $.parseJSON(data);
		if(temp.status == 0){
			if(temp.id != null){
				wordId = temp.id;
				word = temp.wo;
				mean = temp.mean;
				sent = temp.sent;
				$('#word #text').html(word);
				$('#hint').html('');
				$('#hint').hide();
				$('#sentence-text').val('');
			}else{
				disableNextBtn();
				wordContentHide();
				$('#word #text').html('Word list exhausted. Reset list');
			}
		}else{
			/*Some other error*/
			disableNextBtn();
			wordContentHide();
			$('#word #text').html('Unexpected Error.');
		}
		meanShown = 0;
	});
}

function disableNextBtn() {
	$('#nextBtn').attr('disabled',true);
	$('#nextBtn').css('opacity',0.7);
	$('#nextBtn').css('color', '#747474');
};

function enableNextBtn() {
	$('#nextBtn').attr('disabled',false);
	$('#nextBtn').css('opacity',1);
	$('#nextBtn').css('color', '#5A1F4B');
};

function wordContentHide() {
	$('#word #options').hide();
	$('#hint').hide();
	$('#sentence').hide();
};

function wordContentShow() {
	$('#word #options').show();
	$('#hint').show();
	$('#sentence').show();
};

$('#showMean').click(function (){
	if($('#hint').css('display') !== 'none'){
		if(lastclicked == 1){
			$('#hint').slideUp(function(){
				$('#hint').html(mean);
				$('#hint').slideDown();
			});
		}
	}else{
		$('#hint').html(mean);
		$('#hint').slideDown();
	}
	lastclicked = 0;
	meanShown = 1;
});

$('#showSent').click(function (){
	if($('#hint').css('display') !== 'none'){
		if(lastclicked == 0){
			$('#hint').slideUp(function(){
				$('#hint').html(sent);
				$('#hint').slideDown();
			});
		}
	}else{
		$('#hint').html(sent);
		$('#hint').slideDown();
	}
	lastclicked = 1;
});

$('#resetWrdBtn').click(function (){
	$.post('scripts/initdata.php', function(data){
		if(data == 0){
			fetchWord();
			enableNextBtn();
			wordContentShow();
		}else{
			disableNextBtn();
			wordContentHide();
			$('#word #text').html('Unexpected Error.');
		}
	});
	meanShown = 0;
});

$('#nextBtn').click(function(){
	$.post('scripts/modifywordusedata.php',{ word_id: wordId.toString(), mean_show: meanShown.toString() },function(data){
		if(data == 0){
			fetchWord();
		}else{
			disableNextBtn();
			wordContentHide();
			$('#word #text').html('Unexpected Error.');
		}
	});
	meanShown = 0;
});

$(document).ready(function () {
	meanShown = 0;
	enableNextBtn();
	$('#sentence-text').watermark('Here you must enter a SENTENCE that is relevant to the context of the usage of the word entered above.');
	$.post('scripts/login.php', function(data){
	if(data != 'fail' && data != ''){
		loginStatus = 1;
		userName = data;
		$('.userdisp').show();
		$('#user').text(userName);
		fetchWord();
	}else{
		loginStatus = 0;
		window.location.replace('http://localhost/gre');
	}		
	});	
});
